package com.yanqu.road.server.logic.recharge.limit.activity;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.mall.LimitBuyResult;
import com.yanqu.road.entity.recharge.FindActivityResult;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.webservice.GamePublicFunction;

import java.math.BigInteger;

public class RechargeActivityMallLimit extends BaseActivityMallLimit{
    private int conditionId;
    public RechargeActivityMallLimit(long userId, MallInfo mallInfo, ActivityInfo activityInfo) {
        super(userId, mallInfo, activityInfo);
    }

    @Override
    protected int activityCheck() {
        FindActivityResult findResult = GamePublicFunction.findActivityMall(mallInfo.getMallId());
        if(!findResult.isFind()){
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        this.conditionId = findResult.getActivityConditionInfo().getConditionId();
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(findResult.getActivityId());
        this.activityInfo = activityInfo;
        if(!ActivityMgr.activityInTime(activityInfo)){
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        if(activityInfo.getType() != eActivityType.RechargeActivity.getValue() &&
            activityInfo.getType() != eActivityType.NewRechargeActivity.getValue()){
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        return 0;
    }

    @Override
    public boolean isSystemUnLock() {
        if(activityInfo.getType() == eActivityType.NewRechargeActivity.getValue()){
            return SystemOpenMgr.systemOpen(player, GamePublicFunction.getSystemIdNewRechargeActivity(activityInfo.getChildType()));
        }else {
            return SystemOpenMgr.systemOpen(player, eSystemId.RechargeActivity.getValue());
        }
    }

    @Override
    public LimitBuyResult buyCountLimit() {
        LimitBuyResult limitBuyResult = new LimitBuyResult();
        ActivityModule activityModule = player.getModule(ActivityModule.class);
        BaseActivityData baseActivityData = activityModule.getBaseActivityData(activityInfo.getActivityId());
        if (null == baseActivityData) {
            limitBuyResult.setRet(GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY);
            return limitBuyResult;
        }
        UserActivityConditionData userActivityConditionData = baseActivityData.getUserActivityConditionData(conditionId);
        if(null == userActivityConditionData){
            limitBuyResult.setRet(GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY);
            return limitBuyResult;
        }
        if (activityInfo.getType() == eActivityType.NewRechargeActivity.getValue()) {
            if (userActivityConditionData.getValue().compareTo(BigInteger.ONE) >= 0) {
                limitBuyResult.setRet(GameErrorCode.E_MALL_LIMIT_BUY);
                return limitBuyResult;
            }
            limitBuyResult.setLimitCount(1);
        } else {
            ActivityConditionInfo activityConditionInfo = NormalActivityMgr.getActivityConditionInfo(activityInfo.getActivityId(), conditionId);
            if (null == activityConditionInfo) {
                limitBuyResult.setRet(GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY);
                return limitBuyResult;
            }
            BigInteger param2 = activityConditionInfo.getParamList().get(1);
            if (userActivityConditionData.getValue().compareTo(activityConditionInfo.getParamList().get(1)) >= 0) {//限购
                limitBuyResult.setRet(GameErrorCode.E_MALL_LIMIT_BUY);
                return limitBuyResult;
            }
            int limitCount = param2.subtract(userActivityConditionData.getValue()).intValue();
            if(limitCount < 0){
                limitCount = 0;
            }
            limitBuyResult.setLimitCount(limitCount);
        }
        return limitBuyResult;
    }

    @Override
    public int logicCheck() {
        return 0;
    }
}
